package com.sne.airways.basecore.utils;

import com.sne.airways.basecore.exceptions.AppException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: 卞京帅 {bjslogin#gmail.com}
 * Date: 12-11-11
 * Time: 下午5:33
 * To change this template use File | Settings | File Templates.
 */
public class DateHelper extends DateUtils {

    public static final String MONTHPATTERN = "yyyyMM";
    public static final String SIMPLEDATEPATTERN = "yyyyMMdd";
    public static final String DATEPATTERN = "yyyy-MM-dd";
    public static final String DATETIMEPATTERN = "yyyy-MM-dd HH:mm:ss";


    /**
     * 转换日期为字符串形式,
     * @param date 日期
     * @param pattern 上面所列静态日期格式
     * @return string result
     */
    public static String convertDate2String(String pattern,Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        String result = "";
        if (date != null) {
            result = dateFormat.format(date);
        }
        return result;
    }

    /**
     * 将日期转换为字符串
     * @param date date
     * @return string result
     */
    public static String convertDate2String(Date date){
        return convertDate2String(DATEPATTERN,date);
    }

    /**
     * 得到一天的最早的时间
     * @param date one day
     * @return date
     */
    public static Date getDateFirstTime(Date date){
        if(date == null) return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return new Date(cal.getTimeInMillis());
    }

    /**
     * 获取一天最晚的时间
     * @param date one day
     * @return date
     */
    public static Date getDateLastTime(Date date){
        if(date == null) return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 0);
        return new Date(cal.getTimeInMillis());
    }

    /**
     * 转换字符串为日期
     * @param aMask 格式
     * @param strDate 日期字符串
     * @return date
     */
    public static Date convertString2Date(String aMask, String strDate){
        if(StringUtils.isBlank(strDate)) return null;
        SimpleDateFormat df = new SimpleDateFormat(aMask);
        try {
            return new Date(df.parse(strDate).getTime());
        } catch (ParseException pe) {
            throw new AppException("转换日期错误!");
        }
    }


    /**
     * 将日期字符串转为默认日期格式
     * @param dateStr 日期字符串
     * @return date
     */
    public static Date convertString2Date(String dateStr){
        return convertString2Date(DATEPATTERN,dateStr);
    }


    /**
     * 获取当前日期字符串,simpleDatePattern格式
     * @return  string
     */
    public static String getCurrentSimpleDateString(){
        return convertDate2String(SIMPLEDATEPATTERN,new Date());
    }

    /**
     * 获取当前日期字符串  datePattern格式
     * @return date
     */
    public static String getCurrentDateString(){
        return convertDate2String(DATEPATTERN,new Date());
    }

    public static Date getCurrentDate(){
        return new Date();
    }

    /**
     * 获取传入日期所在周的起始日期
     * @param date  date
     * @return 当前日期所在周起始日期
     */
    public static Date getWeekStartDay(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
            cal.add(Calendar.DATE,-7);
            return cal.getTime();
        }
        cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        return cal.getTime();
    }

    /**
     * 获取传入日期所在周的结束日期
     * @param date  date
     * @return 当前日期所在周结束日期
     */
    public static Date getWeekEndDay(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            return date;
        }
        cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
        cal.add(Calendar.DATE,7);
        return cal.getTime();
    }

    /**
     * 获取传入日期所在月份的开始日期
     * @param date  date
     * @return 日期所在月份的开始日期
     */
    public static Date getMonthStartDay(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE,cal.getActualMinimum(Calendar.DATE));
        return cal.getTime();
    }

    /**
     * 获取传入日期所在月份的结束日期
     * @param date  date
     * @return 日期所在月份的结束日期
     */
    public static Date getMonthEndDay(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE,cal.getActualMaximum(Calendar.DATE));
        return cal.getTime();
    }

    /**
     * 滚动日期,
     * @param point 要滚动的时间
     * @param field 要滚动的类别,传入参数为Calendar.YEAR,Calendar.MONTH,DATE...等
     * @param num 滚动数量,正负数都可以,负数为向前滚动,
     * @return  结果
     * 例: DateHelper.rollDate(new Date(),Calendar.DATE,-12);  当前日期的前12天
     * 例: DateHelper.rollDate(new Date(),Calendar.MONTH,2);  向后滚动两个月
     */
    public static Date rollDate(Date point,int field,int num){
        Calendar cal = Calendar.getInstance();
        cal.setTime(point);
        cal.add(field,num);
        return cal.getTime();
    }

    public static void main(String[] args){
        System.out.println(convertDate2String(getMonthStartDay(convertString2Date("2012-01-01"))));
        System.out.println(convertDate2String(getMonthEndDay(convertString2Date("2011-12-01"))));
    }
}
